package com.neusoft.ysu.populationmanagement.util;

import com.neusoft.ysu.populationmanagement.entity.CommonResult;
import com.neusoft.ysu.populationmanagement.entity.PoliceStation;
import com.neusoft.ysu.populationmanagement.service.IPoliceStationService;
import com.neusoft.ysu.populationmanagement.service.IResPopService;
import com.neusoft.ysu.populationmanagement.vo.VResPop;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.Period;
import java.util.List;
import java.util.Random;

@Service
public class GenerateDate {
    @Autowired
    private IPoliceStationService policeStationService;
    @Autowired
    private IResPopService iResPopService;

    public static void main(String[] args) {
//        for (int i = 0; i < 1000; i++) {
//            System.out.println(generateChineseCharacters(2));
//            System.out.println(generateChineseCharacters(3));
//        }
        for (int i = 0; i < 1000; i++) {
            System.out.println(generateRandomLongInRange(0, 4));
        }

    }

    public CommonResult generatepop(Integer num) {
        Random r = new Random();
        num = 1;
        for (int i = 0; i < num; i++) {
            VResPop vResPop = new VResPop();
            vResPop.setBirthday(LocalDate.of(1990 + r.nextInt(33), 1 + r.nextInt(12), 1 + r.nextInt(28)));
            vResPop.setGenderId(r.nextInt(1) + 1);
            vResPop.setHousenum("123456789");// 统一一个户，省的立户了，只不过这个户人有点多了
            // 随机生成姓名
            vResPop.setName(generateChineseCharacters(r.nextInt(2) + 2));

            vResPop.setNationId(generateRandomLongInRange(1, 4));
            // 检索所有警局，随机选择一个
            List<PoliceStation> policeList = (List<PoliceStation>) (policeStationService.selectAllPolice().getResult());
            PoliceStation p = policeList.get(r.nextInt(policeList.size()));

//            vResPop.setIdnum(iResPopService.generateIdNum(vResPop, p));
        }
        return null;
    }

    /**
     * 范围是 [min,max)
     *
     * @param min
     * @param max
     * @return java.lang.Long
     * @author 赵子鉴
     * @date 2024-06-25 18:12
     */
    public static Long generateRandomLongInRange(long min, long max) {
        Random random = new Random();
        return min + (long) (random.nextDouble() * (max - min));
    }

    private static final String FIRST_CHARACTERS = "赵钱孙李周吴郑王冯陈褚卫蒋沈韩杨朱秦尤许何吕施张孔曹严华金魏陶姜戚谢邹喻柏" +
            "水窦章云苏潘葛奚范彭郎鲁韦昌马苗凤花方俞任袁柳酆鲍史唐费廉岑薛雷贺倪汤滕殷罗毕郝邬安常乐于时傅皮卞齐康伍余元卜顾孟平黄和穆萧尹" +
            "姚邵湛汪祁毛禹狄米贝明臧计伏成戴谈宋茅庞熊纪舒屈项祝董梁杜阮蓝闵席季麻强贾路娄危江童颜郭梅盛林刁钟徐邱骆高夏蔡田樊胡凌霍虞万" +
            "支柯昝管卢莫经房裘缪干解应宗丁宣贲邓郁单杭洪包诸左石崔吉钮龚程嵇邢滑裴陆荣翁荀羊於惠甄曲家封芮羿储靳汲邴糜松井段富巫乌焦巴弓" +
            "牧隗山谷车侯宓蓬全郗班仰秋仲伊宫宁仇栾暴甘钭厉戎祖武符刘景詹束龙叶幸司韶郜黎蓟薄印宿白怀蒲邰从鄂索咸籍赖卓蔺屠蒙池乔阴鬱胥" +
            "能苍双闻莘党翟谭贡劳逄姬申扶堵冉宰郦雍郤璩桑桂濮牛寿通边扈燕冀郏浦尚农温别庄晏柴瞿阎充慕连茹习宦艾鱼容向古易慎戈廖庾终暨居" +
            "衡步都耿满弘匡国文寇广禄阙东欧殳沃利蔚越夔隆师巩厍聂晁勾敖融冷訾辛阚那简饶空曾毋沙乜养鞠须丰巢关蒯相查后荆红游竺权逯盖益桓公" +
            "万俟司马上官欧阳夏侯诸葛闻人东方赫连皇甫尉迟公羊澹台公冶宗政濮阳淳于单于太叔申屠公孙仲孙轩辕令狐钟离宇文长孙慕容鲜于闾丘司徒" +
            "司空丌官司寇仉督子车颛孙端木漆雕乐正壤驷公良拓跋夹谷宰父谷梁晋楚闫法汝鄢涂钦段干百里东郭南门呼延归海羊舌微生岳帅缑亢况郈有琴" +
            "梁丘左丘东门西门商牟佘佴伯赏南宫墨哈谯笪年爱阳佟第五言福";
    private static final int MIN_FIRST_CHARACTER_INDEX = 0;
    private static final int MAX_FIRST_CHARACTER_INDEX = FIRST_CHARACTERS.length() - 1;

    public static String generateChineseCharacters(int count) {
        StringBuilder sb = new StringBuilder();
        Random random = new Random();

        // 生成第一个汉字
        int firstCharIndex = random.nextInt(MAX_FIRST_CHARACTER_INDEX - MIN_FIRST_CHARACTER_INDEX + 1) + MIN_FIRST_CHARACTER_INDEX;
        sb.append(FIRST_CHARACTERS.charAt(firstCharIndex));

        // 生成剩余的汉字
        for (int i = 1; i < count; i++) {
            sb.append(generateChineseCharacter());
        }
        return sb.toString();
    }

    private static String generateChineseCharacter() {
        String str = "";
        int highPos, lowPos;

        // 随机生成汉字的两个字节
        Random random = new Random();
        highPos = (176 + Math.abs(random.nextInt(39))); //获取高位值
        lowPos = (161 + Math.abs(random.nextInt(93))); //获取低位值

        byte[] b = new byte[2];
        b[0] = (new Integer(highPos)).byteValue();
        b[1] = (new Integer(lowPos)).byteValue();

        try {
            str = new String(b, "GB2312"); //转换成汉字
        } catch (Exception e) {
            e.printStackTrace();
        }

        return str;
    }


    private static void validateIDCard(String idCard) {
        // 验证身份证号长度和格式
        if (idCard.length() != 18 || !idCard.matches("^\\d{17}[0-9Xx]$")) {
            System.out.println("身份证号格式不合法");
            return;
        }

        // 验证出生日期合法性
        int year = Integer.parseInt(idCard.substring(6, 10));
        int month = Integer.parseInt(idCard.substring(10, 12));
        int day = Integer.parseInt(idCard.substring(12, 14));
        LocalDate birthDate = LocalDate.of(year, month, day);
        if (birthDate.isAfter(LocalDate.now())) {
            System.out.println("身份证号的出生日期不合法");
            return;
        }

        // 验证最后一位校验码
        int[] weightFactors = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
        int sum = 0;
        for (int i = 0; i < 17; i++) {
            sum += Integer.parseInt(String.valueOf(idCard.charAt(i))) * weightFactors[i];
        }
        int checkDigit = sum % 11;
        char checkCode = idCard.charAt(17);
        if (checkDigit == 0 && checkCode != '1' || checkDigit != 0 && checkCode != String.valueOf(12 - checkDigit).charAt(0)) {
            System.out.println("身份证号的校验码不合法");
            return;
        }

        // 计算年龄
        int age = Period.between(birthDate, LocalDate.now()).getYears();
        System.out.println("身份证号合法,持有人年龄为: " + age);
    }

}
